package cn.com.xky.restapi.web;

import cn.com.xky.restapi.domain.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by EKEYXIA on 2017/8/10.
 */
@RestController
@RequestMapping(value = "/users")
public class Controller {

    public static final Map<Long,User> map = new HashMap<>();


    @ApiOperation(value = "新增用户",notes = "新增用户")
    @ApiImplicitParam(name = "user",value = "用户的对象",dataType = "User",required = true)
    @PostMapping(value = "/")
    public void add(@RequestBody User user){
        map.put(user.getId(),user);
    }


    @ApiOperation(value = "删除用户",notes = "根据用户的id删除用户")
    @ApiImplicitParam(name = "id",value = "用户的id",dataType = "Long",required = true, paramType = "path")
    @DeleteMapping(value = "/{id}")
    public void delete(@PathVariable Long id){
        map.remove(id);
    }


    @ApiOperation(value = "更新用户",notes = "根据用户的id更新用户")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id",value = "用户的id",dataType = "Long",required = true,paramType = "path"),
                    @ApiImplicitParam(name = "user",value = "用户的对象",dataType = "User",required = true)
            }
    )
    @PutMapping(value = "/{id}")
    public void modify(@PathVariable Long id, @RequestBody User user){
        User u = map.get(id);
        u.setAge(user.getAge());
        u.setName(user.getName());
        map.put(id,user);
    }

    @ApiOperation(value = "查询所有用户",notes = "查询所有的用户")
    @GetMapping(value = "/")
    public Map<Long, User> getAll(){
        return map;
    }

    @ApiOperation(value = "查询某个用户",notes = "根据id查询某个用户")
    @ApiImplicitParam(name = "id",value = "用户的id",dataType = "Long", required = true, paramType = "path")
    @GetMapping(value = "/{id}")
    public User getOne(@PathVariable Long id){
        return map.get(id);
    }



}
